package com.woniuxy.shop.service;

import java.util.List;

import com.woniuxy.shop.dao.AddressDao;
import com.woniuxy.shop.entity.Address;
import com.woniuxy.shop.exception.DaoException;
import com.woniuxy.shop.exception.NotExistUserException;
import com.woniuxy.shop.exception.OverAddressNumOfSameUserException;
import com.woniuxy.shop.exception.ServiceException;

/**
*@author 作者：
*version 创建时间：
*类说明：地址的业务逻辑处理类
*/
public class AddressService {
	AddressDao ad = new AddressDao();
	public void addAddress(Address address) {
		int getAddressNum;
		int getUserNum;
		try {
			//查看输入的用户ID是否存在
			getUserNum = (int)ad.queryUserNum(address.getUserid());
			getAddressNum = ad.getUserAllAddress(address.getUserid()).size();
			if(getUserNum==0) {
				throw new NotExistUserException("该用户不存在");
			}
//			getAddressNum = ad.getNumOfAddress(address.getUserid());
			if(getAddressNum==5) {//同一用户最多只能有5个地址
				throw new OverAddressNumOfSameUserException("同一用户地址数量不能超过5");
			}
			//如果最开始地址为0，设置为默认地址
			if(getAddressNum==0) {
				address.setIsDefault("是");
			}else {
				//查看其地址默认若设置为“是”，则将其他地址修改为非默认；
				if(address.getIsDefault().equals("是")) {
					ad.alterDefaultAddress(address.getUserid(), address.getId());
				}
			}
			ad.addAddress(address);
		} catch (DaoException e) {
			e.printStackTrace();
			throw new ServiceException("系统出错",e);
		}
	}
	
	
	/**
	 * 更新地址
	 * @param address
	 */
	public void update(Address address) {
		//address的ID和用户ID都不是用户提供，故此时可以不管？
		try {
			//判断新地址是否有设置为默认，如果设置了更改其他地址为非默认
			if(address.getIsDefault().equals("是")) {
				ad.alterDefaultAddress(address.getUserid(), address.getId());
			}
			ad.updateAddress(address);
		}catch(DaoException e) {
			e.printStackTrace();
			throw new ServiceException("系统出错",e);
		}
	}
	
	public void delete(int id) {
		//如果该地址是默认地址，则随机选一个作为默认地址
		try {
			Address address = ad.getAddressByID(id);//先查询ID对应的地址
			if(address.getIsDefault().equals("是")) {
				List<Address> list = ad.getUserAllAddress(address.getUserid());
				if(list.size()!=0) {
					ad.chooseDefaultAddress(list.get(0).getId());
				}
			}
			ad.deleteAddress(address.getId());
			
		} catch (DaoException e) {
			throw new ServiceException("系统出错",e);
		}
	}

	/**
	 * 返回指定用户id的所有地址
	 * @param userid
	 * @return
	 */
	public List<Address> getAddressByUserId(int userid) {
		List<Address> addresses = null;
		try {
			addresses = ad.getUserAllAddress(userid);
		} catch (DaoException e) {
			throw new ServiceException("系统出错",e);
		}
		return addresses;
	}
	
	/**
	 * 将指定id的地址设为默认
	 * @param id
	 */
	public void setDefault(int id) {
		try {
			//得到该id的信息
			Address a = ad.getAddressByID(id);
			//将此id设为默认
			ad.chooseDefaultAddress(id);
			//将不是该id的设为非默认
			ad.alterDefaultAddress(a.getUserid(), id);
		} catch (DaoException e) {
			throw new ServiceException("系统出错",e);
		}
	}

	
	/**
	 * 根据地址id查询地址
	 * @param id
	 * @return
	 */
	public Address getAddressById(int id) {
		Address	 a = null;
		try {
			a = ad.getAddressByID(id);
		} catch (DaoException e) {
			throw new ServiceException("系统出错",e);
		}
		return a;
	}
	
	


	
}
